package com.example.njut;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class GestureActivity extends AppCompatActivity {
    GestureOverlayView gView;
    GestureLibrary gestureLibrary;

    //声明数组，用来保存所有需要动态开启的权限
    private static String[] PERMISSIONS_STORGE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };
    //权限的请求编码
    private static int REQUEST_PERMISSION_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture);

        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
            if(ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=
                    PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,PERMISSIONS_STORGE,REQUEST_PERMISSION_CODE);
            }
        }

        //读取程序创建的手势库
        gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/zhanghan");
        /*if(gestureLibrary.load()){
            Intent intent = new Intent();
            intent.setClass(GestureActivity.this,CheckActivity.class);
            startActivity(intent);
        }*else{*/
            Toast.makeText(GestureActivity.this,"请绘制手势图！",Toast.LENGTH_SHORT).show();
            gView = (GestureOverlayView)findViewById(R.id.gView);
            gView.setGestureColor(Color.BLACK);
            gView.setGestureStrokeWidth(30);
            gView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
                @Override
                public void onGesturePerformed(GestureOverlayView gestureOverlayView, final Gesture gesture) {
                    //手势绘制完成后，出现保存页面
                    LayoutInflater lif = LayoutInflater.from(GestureActivity.this);
                    View saveDialog = lif.inflate(R.layout.save,null);
                    final EditText gSaveName = (EditText)saveDialog.findViewById(R.id.gestureName);
                    ImageView sSaveView = (ImageView)saveDialog.findViewById(R.id.imageView2);
                    Bitmap bitmap = gesture.toBitmap(128,128,10,Color.BLACK);
                    sSaveView.setImageBitmap(bitmap);
                    new AlertDialog.Builder(GestureActivity.this)
                            .setView(saveDialog)
                            .setPositiveButton("取消",null)
                            .setNegativeButton("保存", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    GestureLibrary glib = GestureLibraries.fromFile("/mnt/sdcard/zhanghan");
                                    glib.addGesture(gSaveName.getText().toString().trim(),gesture);
                                    glib.save();
                                    Toast.makeText(GestureActivity.this,"手势保存成功!",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent();
                                    intent.setClass(GestureActivity.this,CheckActivity.class);
                                    startActivity(intent);
                                }
                            })
                            .create()
                            .show();
                }
            });
    }
    //1.去除验证按钮
    //2.当没有保存过手势时，点击game，进入到手势绘制界面
    //3.要求手势绘制只能一次
    //4.绘制成功后，界面跳转到验证手势界面
    //5.当已绘制手势，点击game，进入到手势验证界面
}
